<template>
  <div>
    <form action="/">
      <van-search
        v-model="value"
        placeholder="导入成员姓名"
        input-align="center"
        shape="round"
        @search="onSearch1()"
      />
    </form>
    <form action="/">
      <van-dropdown-menu>
        <van-dropdown-item v-model="value1" :options="option1" />
        <van-dropdown-item v-model="value2" :options="option2" />
        <van-dropdown-item v-model="value3" :options="option3" />
        <div class="flex-container">
          <van-button 
        type="info"
        @click.prevent="onSearch2(), check()"
        class="flex-item"
        >查询</van-button
      >
      <van-button type="primary" @click.prevent="checkAll()" class="flex-item">全选</van-button>
      <van-button type="info" @click.prevent="ReverseAll()" class="flex-item">反选</van-button>
        </div>

      </van-dropdown-menu>
      <van-button 
        type="info"
        @click.prevent="showSelectedUsers()"
        class="search-btn"
        >添加</van-button
      >
    </form>

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
    <div v-for="(item, index) in datas" :key="index">
        <van-collapse v-model="activeName" accordion>


<div v-for="user,userIndex in item.user_list" :key="userIndex">
      
      <van-cell
      clickable
      :key="userIndex"
      :title=user.chname
      @click="toggleCheckbox(user.user_id)"
    >
      <template #right-icon>
        <input type="checkbox" :id="'user_' + user.user_id" v-model="result" :value="user.user_id">
      </template>
    </van-cell>
    </div>
        </van-collapse>
      </div>
    </van-list>
  </div>
</template>
  
  <script>

export default {
  name: "Studentlists",
  data() {
    return {
      result: [],
      search: "搜索",
      value: "",
      value1: 0,
      value2: 0,
      value3: 0,
      option1: [
        { text: "年级", value: 0 },
        { text: "2019级", value: 2019 },
        { text: "2020级", value: 2020 },
        { text: "2021级", value: 2021 },
      ],
      option2: [
        { text: "专业", value: 0 },
        { text: "软件工程", value: 1819 },
        { text: "计算机科学与技术", value: 1820 },
        { text: "通信工程", value: 1821 },
      ],
      option3: [
        { text: "班级", value: 0 },
        { text: "1班", value: 401 },
        { text: "2班", value: 402 },
      ],
      activeName: ["1"],

      datas:[
        {
          user_id_list: [1,4,9],
          user_list: [
            {
              user_id: 1,
              username: "user1",
              chname: "张三",
            },
            {
              user_id: 2,
              username: "admin1",
              chname: "李四",
            },
            {
              user_id: 3,
              username: "admin1",
              chname: "王五",
            },
            {
              user_id: 4,
              username: "admin1",
              chname: "赵六",
            },
            {
              user_id: 5,
              username: "admin1",
              chname: "赵六",
            },
            {
              user_id: 6,
              username: "admin1",
              chname: "赵六",
            },
            {
              user_id: 7,
              username: "admin1",
              chname: "赵六",
            },
            {
              user_id: 8,
              username: "admin1",
              chname: "赵六",
            },
            {
              user_id: 9,
              username: "admin1",
              chname: "赵六",
            },
            {
              user_id: 10,
              username: "user1",
              chname: "张三",
            },
            {
              user_id: 11,
              username: "admin1",
              chname: "李四",
            },
            {
              user_id: 12,
              username: "admin1",
              chname: "王五",
            },
          ],
        },
        ],

      fin: 0,
      loading: false,
      finished: false,
      checked: true,
      activeIcon: "https://img01.yzcdn.cn/vant/user-active.png",
      inactiveIcon: "https://img01.yzcdn.cn/vant/user-inactive.png",
    };
  },
  methods: {

check() {
  // 在数据加载完成后，对于每个 user，如果其 user_id 在 user_id_list 中，则将其添加到结果列表中
  this.datas.forEach((item) => {
    item.user_list.forEach((user) => {
      if (item.user_id_list.includes(user.user_id)) {
        // 如果 user_id 在 user_id_list 中，则将其添加到结果列表中
        if (!this.result.includes(user.user_id)) {
          this.result.push(user.user_id);
        }
      }
    });
  });
},


    checkAll() {
      this.result = this.datas[0].user_list.map(user => user.user_id); // 全选
    },
    ReverseAll() {
      this.result = []; // 反选
    },

    showSelectedUsers() {
      console.log(this.result);
    },

    toggleCheckbox(userId) {
      // 根据用户ID切换复选框的选中状态
      const index = this.result.indexOf(userId);
      if (index !== -1) {
        this.result.splice(index, 1); // 取消选中
      } else {
        this.result.push(userId); // 选中
      }
    },
    onSearch1() {
      console.log(this.value);
    },
    onSearch2() {
      console.log(this.value1, this.value2, this.value3);
    },
    onLoad() {
      // 异步更新数据，setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        for (let i = 0; i < 100; i++) {
          this.fin += 1;

        }

        // 加载状态结束
        this.loading = false;
    // 获取所有用户列表的长度
    let totalUserCount = 0;
    this.datas.forEach((item) => {
      totalUserCount += item.user_list.length;
    });

    // 检查是否加载完所有用户
    if (this.fin >= totalUserCount) {
      this.finished = true;
      // 执行其他相关的代码
    }
      }, 1000);
    },
  },
};
</script>
  
  <style scoped>
.blue-text {
  color: skyblue;
}

.red-text {
  color: rgb(116, 193, 0);
}

.search-btn {
  margin-left: 10%;
  margin-top: 10%;
  width: 80%;
  position: absolute;
  bottom: 20%;
}

.img-icon {
  height: 20px;
}

.flex-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* flex:1; */
}

.flex-item {
  flex: 1;
}


input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  -o-appearance: none;
  -ms-appearance: none;
  display: inline-block;
  width: 20px;
  height: 20px;
  transition: border-color 0.3s;
  cursor: pointer;
}

/* 复选框标记的样式 */
input[type="checkbox"]:checked::before {
  content: "✔"; /* 选中后显示✔符号 */
  display: block;
  width: 20px;
  height: 20px;
  text-align: center;
  line-height: 20px;
  color: white;
  border-radius: 50%; /* 使复选框变成圆形 */
  transition: border-color 0.3s;
  background-color: rgb(70, 175, 240); /* 选中后的背景色 */
  cursor: pointer;
}

input[type="checkbox"]:checked::after {
  display: none; /* 显示 ✔ 符号 */
}
</style>